package net.coding.bamboo.wex.fundmanage;

import com.fasterxml.jackson.core.type.TypeReference;
import com.weihui.basic.lang.common.util.DateUtil;
import com.weihui.basic.util.marshaller.json.JsonUtil;
import com.weihui.commom.agreement.enums.DurationType;
import com.weihui.commom.agreement.enums.RepayMode;
import com.weihui.finance.fundmanage.client.request.LoanRequest;
import com.weihui.finance.fundmanage.client.request.PayRequest;
import com.weihui.finance.fundmanage.client.request.PrepayRequest;
import com.weihui.finance.fundmanage.client.request.RefundRecord;
import com.weihui.finance.fundmanage.client.response.LoanResponse;
import com.weihui.finance.fundmanage.client.response.PayResponse;
import com.weihui.finance.fundmanage.client.response.PrepayResponse;
import com.weihui.finance.fundmanage.client.util.FundManageClient;
import com.weihui.finance.fundmanage.core.common.enums.InterestType;
import com.weihui.finance.fundmanage.core.common.enums.PayModel;
import com.weihui.finance.fundmanage.core.common.utils.ObjectMapperUtils;
import net.coding.bamboo.wex.fundmanage.model.Customer;
import org.junit.BeforeClass;
import org.junit.Test;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author : zenghua
 * @version : v0.1 FundManageAsyncClientTest.java
 * @since : 2017/8/2 14:19
 */
public class FundManageAsyncClientTest {
	private static FundManageClient fundManageClient;

	@BeforeClass
	public static void beforeClass() {
		fundManageClient = new FundManageClient();
		//dev
//        fundManageClient.setBaseAddress("http://10.65.178.14:9404/fund-manage");
		//test，
//		fundManageClient.setBaseAddress("http://10.65.209.16:9404/fund-manage");
		//cloud
//        fundManageClient.setBaseAddress("http://10.65.213.11:22/fund-manage");

        fundManageClient.setBaseAddress("http://127.0.0.1:8080");
	}

	/**
	 * 异步放款
	 */
	@Test
	public void testLoan() {
		LoanRequest loanRequest = new LoanRequest();
		loanRequest.setApplyNo("1501654860124");
		loanRequest.setFundPartnerId("300006238081");
//        loanRequest.setChannelCode("KSRCB");
		loanRequest.setMemberId("10000001428392");
		//商户ID，查询会员必填
		loanRequest.setPartnerId("200005485567");
		//身份证密文
		loanRequest.setCertNo("P4127130");
		//姓名密文
		loanRequest.setCustomerName("P4127133");
		loanRequest.setLoanAmount(new BigDecimal("5000"));
		loanRequest.setLoanTerm(3);
		loanRequest.setTermUnit(DurationType.YEAR.name());
		//卡ID
		loanRequest.setLoanCardNo("295140");
		loanRequest.setRepaymentCardNo("295140");
		loanRequest.setInterestType(InterestType.FLOAT);
		loanRequest.setFixedInterestRate(new BigDecimal("6"));
		loanRequest.setFlowInterestRate(new BigDecimal("7"));
		loanRequest.setApplyDate("20170802");
		loanRequest.setRepayMode(RepayMode.EQUIVALENT_PRINCIPAL.name());
		loanRequest.setLoanPurpose("客户信息加密5");
		loanRequest.setLoanCardBankId("ICBC");//102305223298
		loanRequest.setRepaymentCardBankId("ICBC");
		loanRequest.setOverdueRate(new BigDecimal("0.08"));
		loanRequest.setRepaymentDay(2);
		loanRequest.setBillStartDate("20170802");
		Customer customer = new Customer();
		customer.setName("伊布");
		customer.setAge(35);
		loanRequest.setCustomerInfo(ObjectMapperUtils.objectToJsonString(customer));


		TypeReference<LoanResponse> loanResponseTypeReference = new TypeReference<LoanResponse>() {};
		LoanResponse loanResponse = fundManageClient.invoke(loanRequest, null, loanResponseTypeReference);
		String resp = JsonUtil.toJson(loanResponse);
		System.out.println(resp);
//        String json = "{\"returnCode\":null,\"returnMessage\":null,\"extension\":null,\"redirectForm\":null,\"loanNo\":\"97895a39-e4b0-4659-b654-5d6789bfacc7\"}";
//        System.out.println(JsonUtil.parse(json, loanResponseTypeReference));
	}

	/**
	 * 异步落库
	 */
	@Test
	public void testPay() {
		PayRequest payRequest = new PayRequest();
//		payRequest.setChannelCode("ICBC");
		payRequest.setFundPartnerId("300006238081");
		List<RefundRecord> refundRecords = new ArrayList<>();
		RefundRecord refundRecord = new RefundRecord();
		refundRecord.setLoanNo("1501654860121");
		refundRecord.setPayMode(PayModel.PREPAYMENT.name());
		refundRecord.setPayAmount(new BigDecimal("1000"));
		refundRecord.setIssueNumber(3);
		refundRecord.setRepaymentTime(DateUtil.format(new Date(), DateUtil.longFormat));
		//手续费
//        refundRecord.setTipFee(new BigDecimal("200"));
		refundRecords.add(refundRecord);

//        refundRecord = new RefundRecord();
//        refundRecord.setLoanNo("HTWL305206-1831331787001");
//        refundRecord.setPayMode(PayModel.NORMAL.name());
//        refundRecord.setPayAmount(new BigDecimal("20"));
//        refundRecord.setIssueNumber(6);
//        refundRecord.setRepaymentTime(DateUtil.format(new Date(), DateUtil.longFormat));
//        refundRecords.add(refundRecord);

		payRequest.setRefundRecords(refundRecords);

		TypeReference<PayResponse> typeReference = new TypeReference<PayResponse>() {};

		PayResponse payResponse = fundManageClient.invoke(payRequest, null, typeReference);
		System.out.println(JsonUtil.toJson(payResponse));
	}

	/**
	 * 提前还款试算
	 */
	@Test
	public void testPrePay() {
		PrepayRequest prepayRequest = new PrepayRequest();
//        prepayRequest.setChannelCode("KSRCB");
//        prepayRequest.setPrepayAmount(new BigDecimal("10000"));
		prepayRequest.setFundPartnerId("300006238081");
		prepayRequest.setPayMode(PayModel.PREPAYMENT.name());
		prepayRequest.setLoanNo("1501654860121");
		prepayRequest.setIssueNumber(1);

		TypeReference<PrepayResponse> typeReference = new TypeReference<PrepayResponse>() {};

		PrepayResponse prepayResponse = fundManageClient.invoke(prepayRequest, null, typeReference);
		System.out.println(JsonUtil.toJson(prepayResponse));
	}

}
